# coding=UTF-8
# Filename:ServerUI.py
import Tkinter
import tkFont
import socket
import thread
import time
import sys


class serverUI(object):

    title = 'Server'
    local = ''
    port = 9999
    global serverSock
    flag = False

    def __init__(self):
        self.root = Tkinter.Tk()
        self.root.title(self.title)

        # 窗口面板，用4个frame面板布局
        self.frame = [Tkinter.Frame(), Tkinter.Frame(), Tkinter.Frame(), Tkinter.Frame()]

        # 显示消息Text右边的滚动条
        self.chatTextScrollBar = Tkinter.Scrollbar(self.frame[0])
        self.chatTextScrollBar.pack(side=Tkinter.RIGHT, fill=Tkinter.Y)

        # 显示消息Text，并绑定上面的滚动条
        ft = tkFont.Font(family='Fixdsys', size=11)
        self.chatText = Tkinter.Listbox(self.frame[0], width=70, height=18, font=ft)
        self.chatText['yscrollcommand'] = self.chatTextScrollBar.set
        self.chatText.pack(expand=1, fill=Tkinter.BOTH)
        self.chatTextScrollBar['command'] = self.chatText.yview()
        self.frame[0].pack(expand=1, fill=Tkinter.BOTH)

        # 标签，分开消息显示Text和消息输入Text
        label = Tkinter.Label(self.frame[1], height=2)
        label.pack(fill=Tkinter.BOTH)
        self.frame[1].pack(expand=1, fill=Tkinter.BOTH)

        # 输入消息Text的滚动条
        self.inputTextScrollBar = Tkinter.Scrollbar(self.frame[2])
        self.inputTextScrollBar.pack(side=Tkinter.RIGHT, fill=Tkinter.Y)

        # 输入消息Text， 并与滚动条绑定
        ft = tkFont.Font(family='Fixdsys', size=11)
        self.inputText = Tkinter.Text(self.frame[2], width=70, height=8, font=ft)
        self.inputText['yscrollcommand'] = self.inputTextScrollBar.set
        self.inputText.pack(expand=1, fill=Tkinter.BOTH)
        self.inputTextScrollBar['command'] = self.chatText.yview()
        self.frame[2].pack(expand=1, fill=Tkinter.BOTH)

        # 发送信息按钮
        self.sendButton = Tkinter.Button(self.frame[3], text='发送', width=10, command=self.sendMessage)
        self.sendButton.pack(expand=1, side=Tkinter.BOTTOM and Tkinter.LEFT, padx=25, pady=5)

        # 关闭按钮
        self.closeButton = Tkinter.Button(self.frame[3], text='关闭', width=10, command=self.close)
        self.closeButton.pack(expand=1, side=Tkinter.RIGHT, padx=25, pady=5)
        self.frame[3].pack(expand=1, fill=Tkinter.BOTH)

        # 清除聊天内容
        self.clean_upButton = Tkinter.Button(self.frame[1], text='清空', width=10, command=self.clean_up)
        self.clean_upButton.pack(expand=1, side=Tkinter.RIGHT, padx=15, pady=8)
        self.frame[1].pack(expand=1, fill=Tkinter.BOTH)

    # 接受消息
    def receiveMessage(self):
        # 建立Socket连接
        self.serverSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.serverSock.bind((self.local, self.port))
        self.serverSock.listen(15)
        self.buffer_size = 1024
        self.chatText.insert(Tkinter.END, '服务器已就绪......')

        # 循环接受客户端的连接请求
        while True:
            self.connection, self.address = self.serverSock.accept()
            self.flag = True
            while True:
                # 接受客户端发送的消息
                try:
                    self.clientMsg = self.connection.recv(self.buffer_size)
                except Exception:
                    self.chatText.insert(Tkinter.END, '☆--已断开连接--☆\n')
                    self.flag = False
                    break

                if not self.clientMsg:
                    continue
                elif self.clientMsg == "Hi Evan, I'm Jane":
                    self.chatText.insert(Tkinter.END, '★--与Jane成功建立连接--★')
                    self.connection.send('Hi Jane')
                elif self.clientMsg == 'Jane正在重新进行连接...':
                    self.chatText.insert(Tkinter.END, '正在重新进行连接...')
                    self.connection.close()
                    break

                theTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
                self.chatText.insert(Tkinter.END, 'Jane ' + theTime + ':\n')
                self.chatText.insert(Tkinter.END, ' ' + self.clientMsg)

    # 发送消息
    def sendMessage(self):
        # 得到用户在Text中输入的消息
        message = self.inputText.get('1.0', Tkinter.END)
        # 格式化当前的时间
        thetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())

        if self.flag == True:
            # 将消息发送到客户端
            self.chatText.insert(Tkinter.END, 'Evan ' + thetime + ':\n')
            self.chatText.insert(Tkinter.END, ' ' + message + '\n')
            self.connection.send(message)
            # 清空用户在Text中输入的消息
            self.inputText.delete(0.0, message.__len__() - 1.0)
        else:
            # Socket连接没有建立， 提示用户
            self.chatText.insert(Tkinter.END, '您还未与Jane建立连接，Jane无法收到您的消息\n')
            # 清空用户在Text中输入的消息
            self.inputText.delete(0.0, message.__len__()-1.0)

    # 关闭消息窗口并退出
    def close(self):
        sys.exit()

    # 启动线程接受客户端的消息
    def startNewThread(self):
        # 启动一个新线程来接受客户端的消息
        thread.start_new_thread(self.receiveMessage, ())

    # 清空聊天内容
    def clean_up(self):
        self.chatText.delete(1, Tkinter.END)


def main():
    server = serverUI()
    server.startNewThread()
    server.root.mainloop()


if __name__ == '__main__':
    main()
